/**
 * 
 */
package com.ibuonline.search.solr.handler;

import org.apache.solr.common.params.SolrParams;
import org.apache.solr.core.SolrCore;
import org.apache.solr.handler.RequestHandlerBase;
import org.apache.solr.handler.RequestHandlerUtils;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.schema.XscorerFactory;
import org.apache.solr.util.plugin.SolrCoreAware;

import com.ibuonline.search.solr.score.ExpressionXscorerFactory;

/**
 * @author Liuye
 * 
 */
public class XscorerHandler extends RequestHandlerBase implements SolrCoreAware {
	public static final String ENABLE_DEBUG = "enableDebug";
	private static final String COMMAND_RECONFIG = "rc";
	private static final String COMMAND_RELOAD_DATA = "rd";
	private static final String COMMAND_GET = "get";

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.solr.util.plugin.SolrCoreAware#inform(org.apache.solr.core
	 * .SolrCore)
	 */
	@Override
	public void inform(SolrCore core) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.solr.handler.RequestHandlerBase#handleRequestBody(org.apache
	 * .solr.request.SolrQueryRequest,
	 * org.apache.solr.response.SolrQueryResponse)
	 */
	@Override
	public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp)
			throws Exception {
		rsp.setHttpCaching(false);
		SolrParams params = req.getParams();

		String cmd = params.get("command");
		if (cmd == null) {
			cmd = params.get("cmd");
		}
		final XscorerFactory xFactory = req.getCore().getSchema()
				.getXscorerFactory();
		String message = "";
		if (xFactory == null) {
			message = "the XscorerFactory in schema is null.";
			rsp.add("importResponse", message);
			RequestHandlerUtils.addExperimentalFormatWarning(rsp);
			return;
		}
		if (cmd == null) {
			message = "command is null.";
		} else if (COMMAND_RECONFIG.equals(cmd) && !xFactory.isBusy()) {
			xFactory.reConfigure();
			message = "XscorerFactory's configuration re-loaded sucessfully";
		} else if (COMMAND_RELOAD_DATA.equals(cmd) && !xFactory.isBusy()) {
			new Thread() {
				@Override
				public void run() {
					xFactory.refresh();
				}
			}.start();
			message = "Refresh Xscorer Data...";
		} else if (COMMAND_GET.equals(cmd)) {
			if (xFactory instanceof ExpressionXscorerFactory) {
				Long id = Long.parseLong(params.get("id"));
				float outScore = ((ExpressionXscorerFactory) xFactory)
						.getOuterScore(id);
				message = String.valueOf(outScore);
			}
		} else if (xFactory.isBusy()) {
			message = "A command is still running...";
		} else {
			message = "invalid command.";
		}
		rsp.add("statusMessage", xFactory.isBusy() ? "busy" : "idle");
		rsp.add("importResponse", message);
		rsp.add("statusMessages", xFactory.getStatusMessage());
		RequestHandlerUtils.addExperimentalFormatWarning(rsp);

	}

	// //////////////////////SolrInfoMBeans methods //////////////////////

	@Override
	public String getDescription() {
		return "Reconfigure or refresh XscorerFactory.";
	}

	@Override
	public String getSourceId() {
		return "$Id: XscorerHandler.java 1095447 2011-08-06 16:16:06Z StevenLiu $";
	}

	@Override
	public String getVersion() {
		return "1.0";
	}

	@Override
	public String getSource() {
		return "$URL: http://www.moshiling.com/svn/trunk/b2b-solr/src/main/java/com/moshiling/search/solr/handler/XscorerHandler.java $";
	}
}
